#include "BaseGame.h"
#include "Timer.h"
#include <iostream>

//---------------------------------------------------------------------------------------------//
BaseGame::BaseGame(HINSTANCE hInstance) : m_hInstance(hInstance)
{
}
//---------------------------------------------------------------------------------------------//
bool BaseGame::Start()
{
	if(!m_window.Create())
	{
		std::cout << "Create Window Failed " << std::endl;
		return false;
	}
	
	m_graphics.Init(m_window.GetHwnd());

	m_pInput = new DirectInput(m_hInstance , m_window.GetHwnd() );

	if(!m_pInput->init())
	{
		std::cout << "Init Input Failed" << std::endl;
		return false;
	}
	
	Timer::Start();
	
	return OnStart();
}
//---------------------------------------------------------------------------------------------//
void BaseGame::Loop()
{
	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while (msg.message != WM_QUIT && OnUpdate())
	{
		if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		m_graphics.Clear();
		
		m_graphics.BeginScene();
		
		OnDraw();
		
		m_graphics.EndScene();
		
		m_graphics.Present(m_window.GetHwnd());

		m_pInput->reacquire();

		Timer::Measure();
	}
}
//---------------------------------------------------------------------------------------------//
void BaseGame::ShutDown()
{
	m_pInput->deinit();
	delete m_pInput;
	m_pInput = NULL;

	m_graphics.DeInit();
	OnShutDown();
}